
import { LabelUtil } from "./LabelUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class TalkPopMenu extends cc.Component {
    @property(cc.Label)
    label: cc.Label = null;

    @property
    speed: number = 0.1;

    @property(cc.Node)
    talkTintNode: cc.Node = null;

    @property
    moveDuration: number = 0.5;

    @property(cc.Node)
    moveFrom: cc.Node = null;

    @property(cc.Node)
    moveTarget: cc.Node = null;

    @property
    delayMoveDuration: number = 0.1;


    @property(cc.Node)
    fadeTintNode: cc.Node = null;


    private moveFromPosition: cc.Vec3 = null;
    onLoad() {
        //设置文本
        this.label.fontSize = 50;
        this.label.lineHeight = 60;
        this.label.horizontalAlign = cc.Label.HorizontalAlign.LEFT;
        this.label.string = "";
        // this.node.active = false;
        this.moveFromPosition = this.moveFrom.position;
        this.fadeTintNode.opacity = 0;
        this.fadeTintNode.position = new cc.Vec3(540, 1630);
        // this.node.getChildByName();
    }

    public showTalk(str): number {

        this.label.string = "";
        let delayTime = 0;
        this.fadeTintNode.stopAllActions();
        this.fadeTintNode.opacity = 0;

        if (!this.node.active) {

            delayTime = this.delayMoveDuration + this.moveDuration + 0.3;
            this.node.active = true;

            this.scheduleOnce(function () {
                let action1 = cc.moveTo(this.moveDuration, this.moveTarget.position);
                this.moveFrom.runAction(action1);
            }, this.delayMoveDuration);

        }

        let labelSize = new cc.Size(600, 0);
        let size = LabelUtil.measureSize(str, labelSize, this.label.fontSize, this.label.lineHeight);
        // let qipao = this.label.node.parent.getChildByName("qipao");
        // qipao.setContentSize(size.width + 40,size.height+40);
        cc.log(`【对话框】: width：${size.width}, height:${size.height}`);
        let width = size.width;
        if (width < labelSize.width) {
            width = labelSize.width;
        }
        this.label.node.setContentSize(width, size.height);


        let j = 0;
        let currentTxt = "";


        this.schedule(function () {

            if (this.talkTintNode != null) {
                this.talkTintNode.active = true;
            }

            j = j % str.length;
            if (j == 0) {
                currentTxt = "";
            }
            currentTxt = currentTxt + str[j];
            this.label.string = currentTxt;

            j++;
        }, this.speed, str.length - 1, delayTime);

        let printDuration = this.speed * (str.length - 1) + delayTime + 1;

        this.scheduleOnce(function () {

            let seq = cc.repeatForever(
                cc.sequence(
                    cc.fadeIn(1),
                    cc.fadeOut(1),
                    cc.delayTime(1),
                ));

            this.fadeTintNode.runAction(seq);
        }, printDuration);

        return printDuration + 1;


    }

    public dealClickTalk(canShow: boolean): void {
        if (!canShow) {

            this.node.active = false;
            this.label.string = "";
            this.moveFrom.position = this.moveFromPosition;
            this.fadeTintNode.stopAllActions();
            this.fadeTintNode.opacity = 0;
            if (this.talkTintNode) {
                this.talkTintNode.active = false;
            }
        }

    }

}
